package com.vf.cloud.paas.app.master.api.actor.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.vf.cloud.paas.app.master.api.actor.service.IActorClassify;
import com.vf.cloud.paas.app.master.common.domain.Actor;
import com.vf.cloud.paas.app.master.common.domain.ActorClassify;

@Service
public class ActorClassifyImpl implements IActorClassify {

	public List<ActorClassify> getChildrens(ActorClassify param) {
		if(!StrKit.isBlank(param.getName())) {
			String sql = "SELECT T.*, (CASE  WHEN (SELECT COUNT(ID) AS COUNT FROM ue_actor_classify WHERE PID=T.ID)>0 THEN TRUE ELSE  FALSE END ) AS leaf  FROM ue_actor_classify  T WHERE T.PID=?  and t.name like '%"+param.getName()+"%'   ORDER BY T.SORT ASC";
			return ActorClassify.dao.find(sql, param.getPid());
		}else {
			String sql = "SELECT T.*, (CASE  WHEN (SELECT COUNT(ID) AS COUNT FROM ue_actor_classify WHERE PID=T.ID)>0 THEN TRUE ELSE  FALSE END ) AS leaf  FROM ue_actor_classify  T WHERE T.PID=?    ORDER BY T.SORT ASC";
			return ActorClassify.dao.find(sql, param.getPid());
		}
	}


	@Override
	public void delete(String id) {
		 deleteChildrens(id);
	}

	public void deleteChildrens(String id) {
		List<ActorClassify> assetClassifys = ActorClassify.dao.find("select id from "+ActorClassify.TABLE_NAME+" where pid=? ",id);
		for (ActorClassify classify : assetClassifys) {
			if(classify.delete()) {
				deleteChildrens(classify.getId());
				Db.delete("delete From "+Actor.TABLE_NAME+" where classify_id=? ",classify.getId());
			}
		}
		Db.delete("delete From "+Actor.TABLE_NAME+" where classify_id=? ",id);
		ActorClassify.dao.deleteById(id);
	}

}
